﻿using FlexFlow.Base.Interface;
using FlexFlow.Model;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.DataProvider.MongoDB
{
    /// <summary>
    /// 为文件描述提供MongoDb存储支持
    /// </summary>
    public class MongoDbFileDescriptionProvider : MongoDBPhysicalModelProvider<FileDescription>, IFileDescriptionProvider
    {
        /// <summary>
        /// 创建一个新的文件描述
        /// </summary>
        /// <returns>
        /// 创建的文件描述实例
        /// </returns>
        public override FileDescription Create()
        {
            return new FileDescription()
            {
                Path = "\\",
                Locked = false,
                ReadOnly = false,
                UploadTime = DateTime.Now
            };
        }
        /// <summary>
        /// 取得指定上传者上传的所有文件
        /// </summary>
        /// <param name="uploader">
        /// 要取得上传文件的上传者ID
        /// </param>
        /// <param name="path">
        /// 要获取的文件所在的目录
        /// </param>
        /// <returns>
        /// 取得的文件描述列表
        /// </returns>
        public List<FileDescription> GetFiles(Guid uploader, string path = "\\")
        {
            var col = GetCollection();
            FilterDefinition<FileDescription> filterDef = (FilterDefinition<FileDescription>)(m => m.Uploader == uploader && m.Path == path);
            return col.GetAll(filterDef);
        }

        /// <summary>
        /// 更新指定文件描述的最后更改日期
        /// </summary>
        /// <param name="id">
        /// 文件描述ID
        /// </param>
        /// <param name="time">
        /// 要修改成的最后更改日期
        /// </param>
        /// <returns>
        /// 一个值，表示操作是否成功
        /// </returns>
        public bool UpdateModifyTime(Guid id, DateTime time)
        {
            var upDef = BuildUpdateDef(m => m.LastModifyTime, time);
            return Update(id, upDef);
        }
    }
}
